import { Component, ElementRef, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { institutiondates } from '../institution-add-admin/institution-add-admin.component'
declare var $: any;

@Component({
  selector: 'app-institution-add-ip',
  templateUrl: './institution-add-ip.component.html',
})
export class InstitutionAddIpComponent implements OnInit {

  constructor(private http: HttpClient, private router: Router, public server: Service, private cudTableService: CudTableService) { }

  ngOnInit() {
    this.createUserId = this.server.getuser();
    this.institutioneventEmit = this.server.institutioneventEmit.subscribe((value: any) => {
      this.institutiondate = value;
      //console.log(this.institutiondate)
    });
    this.superiorAdd = "最上级";
  }

  //取消订阅
  public institutioneventEmit: any;
  ngOnDestroy() {
    if (this.institutioneventEmit) {
      this.institutioneventEmit.unsubscribe();
    }
  }
  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();

  //发射引导值
  @Output()
  addstepOutput: EventEmitter<any> = new EventEmitter();

  @Input() superiorAdd: any;

  @Input() superiorAddId: any;


  createUserId: any;

  step: number = 0;

  institutiondate: institutiondates = {};
  //datatype
  adddate: addDatatype = {
    createUserId: this.createUserId,
    institutionId: this.institutiondate.institutionId,
    ruleName: "",
    sortIndex: "",
    ruleMode: true,
    enabled: true,
    expression: ""
  };
  stepadds: any;
  submitAdd(val) {
    //console.log(val)
    this.adddate.createUserId = this.createUserId;
    this.adddate.institutionId = this.institutiondate.institutionId;
    this.adddate.ruleMode = Number(this.adddate.ruleMode);
    this.adddate.enabled = Number(this.adddate.enabled)
    let url = this.server.baseUrl + 'admin/iprules/addIprules';
    let data: addDatatype = this.adddate;
    //console.log(data, this.superiorAddId)
    this.cudTableService.postRequest(url, data).then((res: any) => {
      if (res.result_code === '0000') {
        //prompt
        this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message };
        this.server.promptclose;
        this.addOutput.emit(res.result_code);
        //新增IP规则
        if (val == 1) {
          this.stepadds = { step: 5, treeopp: true, title: "完成", btn: "完成" }
          this.addstepOutput.emit(this.stepadds);
          $("#ipAdd").modal("hide");
        }
        //update
      } else {
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message };
        this.server.promptclose;
      }
    }).catch()
  }

}
export class addDatatype {
  createUserId: string;
  institutionId: String;
  ruleName: string;
  sortIndex: string;
  ruleMode: any;
  enabled: any;
  expression: string;
}